package com.ruoyi.system.utils;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.DocumentFragment;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.io.StringWriter;

public class test {

    public static String getDivContentWithHtml(String svgXml, String xpathExpression) {
        try {
            // 解析SVG文本为DOM对象
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(svgXml.getBytes(StandardCharsets.UTF_8)));

            document.getDocumentElement().normalize();

            // 使用XPath查询目标元素
            XPath xpath = XPathFactory.newInstance().newXPath();
            NodeList targetNodes = (NodeList) xpath.evaluate(xpathExpression, document, XPathConstants.NODESET);

            if (targetNodes.getLength() > 0) {
                Node targetNode = targetNodes.item(0);
                if (targetNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element targetElement = (Element) targetNode;
                    return nodeContentToString(targetElement);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String nodeContentToString(Node node) {
        try {
            StringWriter sw = new StringWriter();
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "no"); // 不需要缩进

            // 创建一个临时的Document来存储子节点
            Document tempDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            DocumentFragment docFragment = tempDoc.createDocumentFragment();

            // 将目标节点的子节点导入到DocumentFragment中
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
                Node importedNode = tempDoc.importNode(child, true);
                docFragment.appendChild(importedNode);
            }

            transformer.transform(new DOMSource(docFragment), new StreamResult(sw));
            return sw.toString();
        } catch (Exception ex) {
            throw new RuntimeException("Error converting node to string", ex);
        }
    }

    public static void main(String[] args) {
        String svgXml =
                "      <div style=\"display: flex;pointer-events: none;align-items: center;justify-content:center;width:100%;height:100%;\">MA-02A<br/>1-VXS-M6-004</div>\n"
                ;

        String xpathExpression = "//div"; // 选择foreignObject下的div元素

        String divContent = getDivContentWithHtml(svgXml, xpathExpression);
        System.out.println(divContent);
    }
}
